package tree.leetcode_222_medium;

public class CountNodes {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        root.right.left = new TreeNode(6);
        int i = new CountNodes().countNodes(root);
        System.out.println(i);
    }
    public int countNodes(TreeNode root){
       //空树返回0
        if(root == null){
            return 0;
        }
        int left = getLevel(root.left);
        int right = getLevel(root.right);
        System.out.println(left);
        if(left == right){
            return countNodes(root.right) + (1 << left);
        }else{
            return countNodes(root.left) + (1 << right);
        }
    }
    public int getLevel(TreeNode root){
        int level = 0;
        while(root != null){
            level++;
            root = root.left;
        }
        return level;
    }
}
